<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备号" prop="objCode">
      <el-input v-model="dataForm.objCode" placeholder="设备号"></el-input>
    </el-form-item>
      <el-form-item label="名称" prop="objUdfchar03">
        <el-input v-model="dataForm.objUdfchar03" placeholder="名称"></el-input>
      </el-form-item>
    <el-form-item label="装置号码" prop="objNumber">
      <el-input v-model="dataForm.objNumber" placeholder="装置号码"></el-input>
    </el-form-item>
    <el-form-item label="GPS位置" prop="objGps">
      <el-input v-model="dataForm.objGps" placeholder="GPS位置"></el-input>
    </el-form-item>


<!--    <el-form-item label="IP" prop="objIp">
      <el-input v-model="dataForm.objIp" placeholder="IP"></el-input>
    </el-form-item>-->
    <el-form-item label="所属项目" prop="objProject">
      <el-input v-model="dataForm.objProject" placeholder="所属项目"></el-input>
    </el-form-item>
    <el-form-item label="所属业主" prop="objOwner">
      <el-input v-model="dataForm.objOwner" placeholder="所属业主"></el-input>
    </el-form-item>
    <el-form-item label="高度" prop="objHeight">
      <el-input v-model="dataForm.objHeight" placeholder="高度"></el-input>
    </el-form-item>
      <el-form-item label="密码" prop="objUdfchar01">
        <el-input v-model="dataForm.objUdfchar01" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="控制器ID" prop="objUdfchar02">
        <el-input v-model="dataForm.objUdfchar02" placeholder="控制器ID"></el-input>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          objId: 0,
          objCode: '',
          objNumber: '',
          objGps: '',
          objIp: '',
          objProject: '',
          objOwner: '',
          objHeight: '',
          objUdfchar01: '',
          objUdfchar02: '',
          objUdfchar03: ''
        },
        dataRule: {
          objCode: [
            { required: true, message: '设备号不能为空', trigger: 'blur' }
          ],
          objNumber: [
            { required: true, message: '装置号码不能为空', trigger: 'blur' }
          ],
          objGps: [
            { required: true, message: 'GPS位置不能为空', trigger: 'blur' }
          ]
          // objIp: [
          //   { required: true, message: 'IP不能为空', trigger: 'blur' }
          // ],
          // objProject: [
          //   { required: true, message: '所属项目不能为空', trigger: 'blur' }
          // ],
          // objOwner: [
          //   { required: true, message: '所属业主不能为空', trigger: 'blur' }
          // ],
          // objHeight: [
          //   { required: true, message: '高度不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.objId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.objId) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmobjects/info/${this.dataForm.objId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.objCode = data.r5rmObjects.objCode
                this.dataForm.objNumber = data.r5rmObjects.objNumber
                this.dataForm.objGps = data.r5rmObjects.objGps
                this.dataForm.objIp = data.r5rmObjects.objIp
                this.dataForm.objProject = data.r5rmObjects.objProject
                this.dataForm.objOwner = data.r5rmObjects.objOwner
                this.dataForm.objHeight = data.r5rmObjects.objHeight
                this.dataForm.objUdfchar01 = data.r5rmObjects.objUdfchar01
                this.dataForm.objUdfchar02 = data.r5rmObjects.objUdfchar02
                this.dataForm.objUdfchar03 = data.r5rmObjects.objUdfchar03
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/udp/r5rmobjects/${!this.dataForm.objId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'objId': this.dataForm.objId || undefined,
                'objCode': this.dataForm.objCode,
                'objNumber': this.dataForm.objNumber,
                'objGps': this.dataForm.objGps,
                'objIp': this.dataForm.objIp,
                'objProject': this.dataForm.objProject,
                'objOwner': this.dataForm.objOwner,
                'objHeight': this.dataForm.objHeight,
                'objUdfchar01': this.dataForm.objUdfchar01,
                'objUdfchar02': this.dataForm.objUdfchar02,
                'objUdfchar03': this.dataForm.objUdfchar03
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
